package com.init.engineering.dao;


import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Branch;
import com.init.engineering.pojo.Project;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface BranchDao {

    @Results(id = "branchMap",value = {
            @Result(column = "pid",property = "pid"),
            @Result(column = "pid",property = "project",javaType = Project.class,one =
            @One(select = "com.init.engineering.dao.ProjectDao.findProjectByPid",fetchType = FetchType.EAGER))
    })
    @Select("SELECT * from branch where status = 1")
    List<Branch> findBranches();


    @ResultMap("branchMap")
    @Select({"<script> SELECT * FROM branch <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and bname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where> limit #{currentPage},#{pageSize}" +
            "</script>"})
    List<Branch> findBranchByPage(QueryPageBean queryPageBean);


    @Select({"<script> SELECT count(*) FROM branch <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and bname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where>" +
            "</script>"})
    long selectTotalCount(@Param("queryString")String queryString);



    @Update("UPDATE branch set status = 0 where bid = #{bid}")
    int updateBranchStatus(@Param("bid")String bid);


    @Update("<script>update branch <set><if test='bname != null'>bname = #{bname},</if>" +
            "<if test='pid != null'>pid = #{pid},</if>" +
            "<if test='updatetime != null'>updatetime = #{updatetime}</if>" +
            "</set> where bid = #{bid}</script>")
    int updateBranch(Branch branch);


    @Insert("insert into branch(bid,bname,pid,updatetime,status)" +
            " values(#{bid},#{bname},#{pid},#{updatetime},1)")
    int saveBranch(Branch branch);

    @Select("select * from Branch where pid = #{pid} and status = 1")
    List<Branch> findBranchByPid(int pid);


    @Select("select * from branch where bid = #{bid} and status = 1")
    Branch findBranchByBid(String bid);


}
